{% extends 'base.html' %}

{% block title %}{{ supplier.name }} - 供应商详情 - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
                <li class="breadcrumb-item"><a href="{% url 'assets:supplier_list' %}">供应商管理</a></li>
                <li class="breadcrumb-item active" aria-current="page">{{ supplier.name }}</li>
            </ol>
        </nav>

        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2><i class="fas fa-truck me-2"></i>{{ supplier.name }}</h2>
            <div class="btn-group">
                <a href="{% url 'assets:supplier_update' supplier.pk %}" class="btn btn-primary">
                    <i class="fas fa-edit me-1"></i>编辑供应商
                </a>
                <button type="button" class="btn btn-outline-primary dropdown-toggle dropdown-toggle-split"
                    data-bs-toggle="dropdown">
                    <span class="visually-hidden">更多操作</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="{% url 'assets:asset_create' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-plus me-2"></i>添加资产
                        </a></li>
                    <li><a class="dropdown-item" href="{% url 'assets:consumable_create' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-boxes me-2"></i>添加耗材
                        </a></li>
                    <li><a class="dropdown-item" href="{% url 'assets:contract_create' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-file-contract me-2"></i>创建合同
                        </a></li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    <li><a class="dropdown-item" href="{% url 'assets:asset_list' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-cube me-2"></i>查看供应商资产
                        </a></li>
                    <li><a class="dropdown-item" href="{% url 'assets:consumable_list' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-boxes me-2"></i>查看供应商耗材
                        </a></li>
                    <li><a class="dropdown-item" href="{% url 'assets:contract_list' %}?supplier={{ supplier.pk }}">
                            <i class="fas fa-file-contract me-2"></i>查看相关合同
                        </a></li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    <li><a class="dropdown-item" href="{% url 'assets:supplier_list' %}">
                            <i class="fas fa-arrow-left me-2"></i>返回列表
                        </a></li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    <li><a class="dropdown-item text-danger" href="{% url 'assets:supplier_delete' supplier.pk %}"
                            onclick="return confirm('确定要删除供应商 {{ supplier.name }} 吗？')">
                            <i class="fas fa-trash me-2"></i>删除供应商
                        </a></li>
                </ul>
            </div>
        </div>

        <div class="row">
            <!-- 基本信息 -->
            <div class="col-md-8">
                <!-- 供应商基本信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-info-circle me-2"></i>供应商基本信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="35%" class="text-muted">供应商名称:</td>
                                        <td><strong>{{ supplier.name }}</strong></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">供应商编号:</td>
                                        <td><code>SUP-{{ supplier.pk|stringformat:"05d" }}</code></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">供应商类型:</td>
                                        <td>
                                            <span class="badge bg-info">{{ supplier.get_supplier_type_display }}</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">统一社会信用代码:</td>
                                        <td>
                                            {% if supplier.credit_code %}
                                            <code>{{ supplier.credit_code }}</code>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">法定代表人:</td>
                                        <td>
                                            {% if supplier.legal_representative %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-user-tie me-1 text-primary"></i>
                                                {{ supplier.legal_representative }}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">联系人:</td>
                                        <td>
                                            {% if supplier.contact_person %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-user me-1 text-success"></i>
                                                {{ supplier.contact_person }}
                                                {% if supplier.contact_position %}
                                                <small class="text-muted ms-1">({{ supplier.contact_position }})</small>
                                                {% endif %}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">联系电话:</td>
                                        <td>
                                            {% if supplier.phone %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-phone me-1 text-success"></i>
                                                <a href="tel:{{ supplier.phone }}" class="text-decoration-none">{{ supplier.phone }}</a>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="35%" class="text-muted">供应商状态:</td>
                                        <td>
                                            <span class="badge bg-success">
                                                <i class="fas fa-check me-1"></i>活跃
                                            </span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">手机号码:</td>
                                        <td>
                                            {% if supplier.mobile %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-mobile-alt me-1 text-primary"></i>
                                                <a href="tel:{{ supplier.mobile }}" class="text-decoration-none">{{ supplier.mobile }}</a>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">邮箱地址:</td>
                                        <td>
                                            {% if supplier.email %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-envelope me-1 text-info"></i>
                                                <a href="mailto:{{ supplier.email }}" class="text-decoration-none">{{ supplier.email }}</a>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">官方网站:</td>
                                        <td>
                                            {% if supplier.website %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-globe me-1 text-info"></i>
                                                <a href="{{ supplier.website }}" target="_blank"
                                                    class="text-decoration-none">{{ supplier.website }}</a>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">传真号码:</td>
                                        <td>
                                            {% if supplier.fax %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-fax me-1 text-secondary"></i>
                                                {{ supplier.fax }}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">创建时间:</td>
                                        <td>
                                            <i class="fas fa-calendar-plus me-1 text-primary"></i>{{ supplier.created_at|date:"Y-m-d H:i" }}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">更新时间:</td>
                                        <td>
                                            <i class="fas fa-calendar-check me-1 text-success"></i>{{ supplier.updated_at|date:"Y-m-d H:i" }}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <!-- 注册地址和经营范围 -->
                        {% if supplier.registration_address %}
                        <div class="mt-3">
                            <h6 class="text-muted">
                                <i class="fas fa-building me-2"></i>注册地址:
                            </h6>
                            <p class="mb-0">{{ supplier.registration_address }}</p>
                        </div>
                        {% endif %}

                        {% if supplier.business_scope %}
                        <div class="mt-3">
                            <h6 class="text-muted">
                                <i class="fas fa-briefcase me-2"></i>经营范围:
                            </h6>
                            <p class="mb-0">{{ supplier.business_scope }}</p>
                        </div>
                        {% endif %}
                        {% if supplier.address %}
                        <div class="mt-3">
                            <h6 class="text-muted">
                                <i class="fas fa-map-marker-alt me-2"></i>联系地址:
                            </h6>
                            <p class="mb-0">{{ supplier.address }}
                                {% if supplier.postal_code %}
                                <span class="text-muted ms-2">邮编: {{ supplier.postal_code }}</span>
                                {% endif %}
                            </p>
                        </div>
                        {% endif %}
                        {% if supplier.description %}
                        <div class="mt-3">
                            <h6 class="text-muted">
                                <i class="fas fa-info-circle me-2"></i>供应商描述:
                            </h6>
                            <p class="mb-0">{{ supplier.description }}</p>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 银行信息 -->
                {% if supplier.bank_name or supplier.bank_account or supplier.bank_address %}
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-university me-2"></i>银行信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="35%" class="text-muted">开户银行:</td>
                                        <td>
                                            {% if supplier.bank_name %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-university me-1 text-primary"></i>
                                                {{ supplier.bank_name }}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">银行账号:</td>
                                        <td>
                                            {% if supplier.bank_account %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-credit-card me-1 text-success"></i>
                                                <code>{{ supplier.bank_account }}</code>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="35%" class="text-muted">开户行地址:</td>
                                        <td>
                                            {% if supplier.bank_address %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-map-marker-alt me-1 text-info"></i>
                                                {{ supplier.bank_address }}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                {% endif %}

                <!-- 合作统计与概览 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-chart-line me-2"></i>合作统计与概览
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">合作时长:</td>
                                        <td>
                                            <i class="fas fa-handshake me-1 text-primary"></i>{{ supplier.created_at|timesince }}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">资产总数:</td>
                                        <td>
                                            <span class="text-primary fw-bold">{{ total_assets|default:0 }} 个</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">耗材种类:</td>
                                        <td>
                                            <span class="text-warning fw-bold">{{ total_consumables|default:0 }}
                                                种</span>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">合同数量:</td>
                                        <td>
                                            <span class="text-success fw-bold">{{ total_contracts|default:0 }} 个</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">资产总值:</td>
                                        <td>
                                            <span class="text-success fw-bold">¥{{ total_asset_value|floatformat:2|default:"0.00" }}</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">合同总额:</td>
                                        <td>
                                            <span class="text-info fw-bold">¥{{ total_contract_value|floatformat:2|default:"0.00" }}</span>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <!-- 统计图表区域 -->
                        <div class="mt-3">
                            <div class="row text-center">
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-primary mb-0">{{ assets_in_use_count|default:0 }}</h6>
                                            <small class="text-muted">在用资产</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-secondary mb-0">{{ assets_idle_count|default:0 }}</h6>
                                            <small class="text-muted">闲置资产</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-success mb-0">{{ active_contracts_count|default:0 }}</h6>
                                            <small class="text-muted">生效合同</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-warning mb-0">{{ expired_contracts_count|default:0 }}</h6>
                                            <small class="text-muted">过期合同</small>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 供应商资质信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-certificate me-2"></i>供应商资质信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">营业执照:</td>
                                        <td>
                                            {% if supplier.business_license %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-file-pdf me-1 text-danger"></i>
                                                <a href="{{ supplier.business_license.url }}" target="_blank"
                                                    class="text-decoration-none">查看营业执照</a>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">未上传</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">联系地址:</td>
                                        <td>
                                            {% if supplier.address %}
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-map-marker-alt me-1 text-info"></i>
                                                {{ supplier.address }}
                                            </div>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">邮政编码:</td>
                                        <td>
                                            {% if supplier.postal_code %}
                                            <code>{{ supplier.postal_code }}</code>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">法定代表人:</td>
                                        <td>
                                            {% if supplier.legal_representative %}
                                            <i class="fas fa-user-tie me-1 text-primary"></i>{{ supplier.legal_representative }}
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">供应商类型:</td>
                                        <td>
                                            <span class="badge bg-info">{{ supplier.get_supplier_type_display }}</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">创建时间:</td>
                                        <td>
                                            <i class="fas fa-calendar-plus me-1 text-info"></i>{{ supplier.created_at|date:"Y-m-d" }}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 经营范围与业务信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-briefcase me-2"></i>经营范围与业务信息
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if supplier.business_scope %}
                        <div class="mb-3">
                            <h6 class="text-muted">
                                <i class="fas fa-list-ul me-2"></i>经营范围:
                            </h6>
                            <p class="mb-0">{{ supplier.business_scope }}</p>
                        </div>
                        {% endif %}

                        {% if supplier.description %}
                        <div class="mb-3">
                            <h6 class="text-muted">
                                <i class="fas fa-info-circle me-2"></i>供应商描述:
                            </h6>
                            <p class="mb-0">{{ supplier.description }}</p>
                        </div>
                        {% endif %}

                        {% if not supplier.business_scope and not supplier.description %}
                        <div class="text-muted text-center py-3">
                            <i class="fas fa-briefcase fa-2x mb-2"></i>
                            <p class="mb-0">暂无经营范围和业务信息</p>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 资质文件管理 -->
                <div class="card mb-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            <i class="fas fa-folder-open me-2"></i>资质文件管理
                        </h5>
                        <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal"
                            data-bs-target="#uploadQualificationModal">
                            <i class="fas fa-upload me-1"></i>上传文件
                        </button>
                    </div>
                    <div class="card-body">
                        {% if supplier.documents.all %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>文件类型</th>
                                        <th>文件名称</th>
                                        <th>文件大小</th>
                                        <th>有效期</th>
                                        <th>上传人</th>
                                        <th>上传时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for file in supplier.documents.all %}
                                    <tr>
                                        <td>
                                            {% if file.file_type == 'business_license' %}
                                            <span class="badge bg-primary">
                                                <i class="fas fa-certificate me-1"></i>营业执照
                                            </span>
                                            {% elif file.file_type == 'tax_registration' %}
                                            <span class="badge bg-info">
                                                <i class="fas fa-file-invoice me-1"></i>税务登记
                                            </span>
                                            {% elif file.file_type == 'quality_cert' %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-award me-1"></i>质量认证
                                            </span>
                                            {% elif file.file_type == 'safety_cert' %}
                                            <span class="badge bg-warning">
                                                <i class="fas fa-shield-alt me-1"></i>安全认证
                                            </span>
                                            {% elif file.file_type == 'other' %}
                                            <span class="badge bg-secondary">
                                                <i class="fas fa-file me-1"></i>其他
                                            </span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-file me-2 text-muted"></i>
                                                <div>
                                                    <strong>{{ file.title }}</strong>
                                                    {% if file.description %}
                                                    <br><small class="text-muted">{{ file.description|truncatechars:50 }}</small>
                                                    {% endif %}
                                                </div>
                                            </div>
                                        </td>
                                        <td>{{ file.file_size_mb }} MB</td>
                                        <td>
                                            {% if file.expiry_date %}
                                            {% if file.is_expired %}
                                            <span class="badge bg-danger">
                                                <i class="fas fa-exclamation-triangle me-1"></i>已过期 ({{
                                                file.expiry_date|date:"Y-m-d" }})
                                            </span>
                                            {% else %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-calendar me-1"></i>{{ file.expiry_date|date:"Y-m-d" }}
                                            </span>
                                            {% endif %}
                                            {% else %}
                                            <span class="badge bg-info">
                                                <i class="fas fa-infinity me-1"></i>长期有效
                                            </span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            {% if file.uploaded_by %}
                                            <i class="fas fa-user me-1 text-info"></i>{{ file.uploaded_by.get_full_name|default:file.uploaded_by.username }}
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ file.created_at|date:"Y-m-d H:i" }}</td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{% url 'assets:supplier_document_view' file.pk %}"
                                                    class="btn btn-outline-primary" title="在线查看"
                                                    data-modal="iframe" data-title="在线查看：{{ file.title }}">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ file.file.url }}" class="btn btn-outline-success" title="下载"
                                                    download>
                                                    <i class="fas fa-download"></i>
                                                </a>
                                                <button type="button"
                                                    class="btn btn-outline-danger"
                                                    title="删除"
                                                    data-confirm="确定要删除文件 &quot;{{ file.title }}&quot; 吗？"
                                                    data-post-url="{% url 'assets:supplier_qualification_delete' file.pk %}">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 文件状态统计 -->
                        <div class="mt-3">
                            <div class="row text-center">
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-primary mb-0">{{ supplier.documents.count }}</h6>
                                            <small class="text-muted">总文件数</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-success mb-0">{{ valid_files_count|default:0 }}</h6>
                                            <small class="text-muted">有效文件</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-warning mb-0">{{ expiring_files_count|default:0 }}</h6>
                                            <small class="text-muted">即将过期</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="card bg-light">
                                        <div class="card-body py-2">
                                            <h6 class="text-danger mb-0">{{ expired_files_count|default:0 }}</h6>
                                            <small class="text-muted">已过期</small>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-folder-open fa-3x mb-3"></i>
                            <h6>暂无资质文件</h6>
                            <p class="mb-3">支持上传营业执照、税务登记证、质量认证等文件</p>
                            <button type="button" class="btn btn-primary" data-bs-toggle="modal"
                                data-bs-target="#uploadQualificationModal">
                                <i class="fas fa-upload me-2"></i>上传第一个文件
                            </button>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 供应商评价与信用 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-star me-2"></i>供应商评价与信用
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">信用等级:</td>
                                        <td>
                                            {% if supplier.credit_rating %}
                                            {% if supplier.credit_rating == "AAA" %}
                                            <span class="badge bg-success fs-6">
                                                <i class="fas fa-star me-1"></i>{{ supplier.credit_rating }}
                                            </span>
                                            {% elif supplier.credit_rating == "AA" %}
                                            <span class="badge bg-primary fs-6">
                                                <i class="fas fa-star me-1"></i>{{ supplier.credit_rating }}
                                            </span>
                                            {% elif supplier.credit_rating == "A" %}
                                            <span class="badge bg-info fs-6">
                                                <i class="fas fa-star me-1"></i>{{ supplier.credit_rating }}
                                            </span>
                                            {% else %}
                                            <span class="badge bg-secondary fs-6">
                                                <i class="fas fa-star me-1"></i>{{ supplier.credit_rating }}
                                            </span>
                                            {% endif %}
                                            {% else %}
                                            <span class="text-muted">未评级</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">综合评分:</td>
                                        <td>
                                            {% if supplier.overall_rating %}
                                            <div class="d-flex align-items-center">
                                                <div class="me-2">
                                                    {% for i in "12345"|make_list %}
                                                    {% if forloop.counter <= supplier.overall_rating %} <i
                                                        class="fas fa-star text-warning"></i>
                                                        {% else %}
                                                        <i class="far fa-star text-muted"></i>
                                                        {% endif %}
                                                        {% endfor %}
                                                </div>
                                                <span class="fw-bold">{{ supplier.overall_rating }}/5</span>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">暂无评分</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">合作满意度:</td>
                                        <td>
                                            {% if supplier.satisfaction_score %}
                                            <div class="progress" style="height: 20px;">
                                                <div class="progress-bar bg-success" role="progressbar"
                                                    style="width: {{ supplier.satisfaction_score }}%"
                                                    aria-valuenow="{{ supplier.satisfaction_score }}" aria-valuemin="0"
                                                    aria-valuemax="100">
                                                    {{ supplier.satisfaction_score }}%
                                                </div>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">暂无数据</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="30%" class="text-muted">交付准时率:</td>
                                        <td>
                                            {% if supplier.delivery_rate %}
                                            <div class="progress" style="height: 20px;">
                                                <div class="progress-bar bg-info" role="progressbar"
                                                    style="width: {{ supplier.delivery_rate }}%"
                                                    aria-valuenow="{{ supplier.delivery_rate }}" aria-valuemin="0"
                                                    aria-valuemax="100">
                                                    {{ supplier.delivery_rate }}%
                                                </div>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">暂无数据</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">质量合格率:</td>
                                        <td>
                                            {% if supplier.quality_rate %}
                                            <div class="progress" style="height: 20px;">
                                                <div class="progress-bar bg-primary" role="progressbar"
                                                    style="width: {{ supplier.quality_rate }}%"
                                                    aria-valuenow="{{ supplier.quality_rate }}" aria-valuemin="0"
                                                    aria-valuemax="100">
                                                    {{ supplier.quality_rate }}%
                                                </div>
                                            </div>
                                            {% else %}
                                            <span class="text-muted">暂无数据</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">最后评价时间:</td>
                                        <td>
                                            {% if supplier.last_evaluation_date %}
                                            <i class="fas fa-calendar me-1 text-muted"></i>{{
                                            supplier.last_evaluation_date|date:"Y-m-d" }}
                                            {% else %}
                                            <span class="text-muted">暂无评价</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        {% if supplier.evaluation_notes %}
                        <div class="mt-3">
                            <h6 class="text-muted">
                                <i class="fas fa-comment me-2"></i>评价备注:
                            </h6>
                            <div class="alert alert-light">
                                <p class="mb-0">{{ supplier.evaluation_notes }}</p>
                            </div>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 相关合同 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-file-contract me-2"></i>相关合同
                            {% if total_contracts > 10 %}
                            <a href="{% url 'assets:contract_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-primary btn-sm float-end">
                                查看全部 {{ total_contracts }} 个合同
                            </a>
                            {% endif %}
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if contracts %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>合同名称</th>
                                        <th>合同编号</th>
                                        <th>合同金额</th>
                                        <th>开始日期</th>
                                        <th>结束日期</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for contract in contracts %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-file-contract me-2 text-primary"></i>
                                                <strong>{{ contract.name }}</strong>
                                            </div>
                                        </td>
                                        <td><code>{{ contract.contract_number }}</code></td>
                                        <td>
                                            <span class="text-success fw-bold">¥{{ contract.amount|floatformat:2
                                                }}</span>
                                        </td>
                                        <td>
                                            <i class="fas fa-calendar-alt me-1 text-success"></i>{{
                                            contract.start_date|date:"Y-m-d" }}
                                        </td>
                                        <td>
                                            <i class="fas fa-calendar-times me-1 text-danger"></i>{{
                                            contract.end_date|date:"Y-m-d" }}
                                        </td>
                                        <td>
                                            {% if contract.status == 'draft' %}
                                            <span class="badge bg-secondary">
                                                <i class="fas fa-edit me-1"></i>草稿
                                            </span>
                                            {% elif contract.status == 'active' %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-check me-1"></i>生效中
                                            </span>
                                            {% elif contract.status == 'expired' %}
                                            <span class="badge bg-warning">
                                                <i class="fas fa-clock me-1"></i>已过期
                                            </span>
                                            {% elif contract.status == 'terminated' %}
                                            <span class="badge bg-danger">
                                                <i class="fas fa-times me-1"></i>已终止
                                            </span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{% url 'assets:contract_detail' contract.pk %}"
                                                    class="btn btn-outline-info" title="查看合同详情">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{% url 'assets:contract_update' contract.pk %}"
                                                    class="btn btn-outline-primary" title="编辑合同">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-file-contract fa-3x mb-3"></i>
                            <h6>暂无相关合同</h6>
                            <p class="mb-3">该供应商尚未签订任何合同</p>
                            <a href="{% url 'assets:contract_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i>创建第一个合同
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 供应商资产 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-cube me-2"></i>供应商资产
                            {% if total_assets > 10 %}
                            <a href="{% url 'assets:asset_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-primary btn-sm float-end">
                                查看全部 {{ total_assets }} 个资产
                            </a>
                            {% endif %}
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if assets %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>资产名称</th>
                                        <th>资产编号</th>
                                        <th>类别</th>
                                        <th>采购价格</th>
                                        <th>启用日期</th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for asset in assets %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-cube me-2 text-primary"></i>
                                                <strong>{{ asset.name }}</strong>
                                            </div>
                                        </td>
                                        <td><code>{{ asset.asset_number }}</code></td>
                                        <td>
                                            {% if asset.category %}
                                            <span class="badge bg-info">{{ asset.category.name }}</span>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="text-success fw-bold">¥{{ asset.purchase_price|floatformat:2
                                                }}</span>
                                        </td>
                                        <td>
                                            <i class="fas fa-calendar me-1 text-muted"></i>{{
                                            asset.purchase_date|date:"Y-m-d" }}
                                        </td>
                                        <td>
                                            {% if asset.status == 'idle' %}
                                            <span class="badge bg-secondary">
                                                <i class="fas fa-pause me-1"></i>闲置
                                            </span>
                                            {% elif asset.status == 'in_use' %}
                                            <span class="badge bg-success">
                                                <i class="fas fa-play me-1"></i>在用
                                            </span>
                                            {% elif asset.status == 'maintenance' %}
                                            <span class="badge bg-warning">
                                                <i class="fas fa-wrench me-1"></i>维修中
                                            </span>
                                            {% elif asset.status == 'scrapped' %}
                                            <span class="badge bg-danger">
                                                <i class="fas fa-trash me-1"></i>已报废
                                            </span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{% url 'assets:asset_detail' asset.pk %}"
                                                    class="btn btn-outline-info" title="查看资产详情">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{% url 'assets:asset_update' asset.pk %}"
                                                    class="btn btn-outline-primary" title="编辑资产">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-cube fa-3x mb-3"></i>
                            <h6>暂无供应商资产</h6>
                            <p class="mb-3">该供应商尚未提供任何资产</p>
                            <a href="{% url 'assets:asset_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i>添加第一个资产
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 供应商耗材 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-boxes me-2"></i>供应商耗材
                            {% if total_consumables > 10 %}
                            <a href="{% url 'assets:consumable_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-primary btn-sm float-end">
                                查看全部 {{ total_consumables }} 种耗材
                            </a>
                            {% endif %}
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if consumables %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead class="table-light">
                                    <tr>
                                        <th>耗材名称</th>
                                        <th>耗材编码</th>
                                        <th>类别</th>
                                        <th>当前库存</th>
                                        <th>采购单价</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for consumable in consumables %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fas fa-box me-2 text-warning"></i>
                                                <strong>{{ consumable.name }}</strong>
                                            </div>
                                        </td>
                                        <td><code>{{ consumable.code }}</code></td>
                                        <td>
                                            {% if consumable.category %}
                                            <span class="badge bg-info">{{ consumable.category.name }}</span>
                                            {% else %}
                                            <span class="text-muted">-</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="fw-bold">{{ consumable.quantity }}</span> {{ consumable.unit }}
                                        </td>
                                        <td>
                                            <span class="text-success fw-bold">¥{{
                                                consumable.purchase_price|floatformat:2 }}</span>
                                        </td>
                                        <td>
                                            <div class="btn-group btn-group-sm">
                                                <a href="{% url 'assets:consumable_detail' consumable.pk %}"
                                                    class="btn btn-outline-info" title="查看耗材详情">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{% url 'assets:consumable_update' consumable.pk %}"
                                                    class="btn btn-outline-primary" title="编辑耗材">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-muted text-center py-4">
                            <i class="fas fa-boxes fa-3x mb-3"></i>
                            <h6>暂无供应商耗材</h6>
                            <p class="mb-3">该供应商尚未提供任何耗材</p>
                            <a href="{% url 'assets:consumable_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i>添加第一个耗材
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 侧边栏信息 -->
            <div class="col-md-4">
                <!-- 供应商状态卡片 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-chart-pie me-2"></i>供应商概览
                        </h5>
                    </div>
                    <div class="card-body text-center">
                        <div class="mb-3">
                            <div class="alert alert-success">
                                <i class="fas fa-truck fa-2x mb-2"></i>
                                <h5>活跃供应商</h5>
                                <p class="mb-0 fw-bold">{{ supplier.name }}</p>
                            </div>
                        </div>

                        <div class="row text-center">
                            <div class="col-4">
                                <h4 class="text-primary">{{ total_assets|default:0 }}</h4>
                                <small class="text-muted">资产数</small>
                            </div>
                            <div class="col-4">
                                <h4 class="text-warning">{{ total_consumables|default:0 }}</h4>
                                <small class="text-muted">耗材种类</small>
                            </div>
                            <div class="col-4">
                                <h4 class="text-success">{{ total_contracts|default:0 }}</h4>
                                <small class="text-muted">合同数</small>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 评价与信用 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-star me-2"></i>评价与信用
                        </h5>
                    </div>
                    <div class="card-body">
                        <!-- 供应商状态 -->
                        <div class="mb-3 text-center">
                            <span class="badge bg-{{ supplier.get_status_color }} fs-6 px-3 py-2">
                                {% if supplier.is_blacklisted %}
                                <i class="fas fa-ban me-1"></i>{{ supplier.get_status_display }}
                                {% elif supplier.is_preferred %}
                                <i class="fas fa-crown me-1"></i>{{ supplier.get_status_display }}
                                {% else %}
                                <i class="fas fa-handshake me-1"></i>{{ supplier.get_status_display }}
                                {% endif %}
                            </span>
                        </div>

                        <!-- 信用等级 -->
                        {% if supplier.credit_rating %}
                        <div class="mb-3 text-center">
                            <h6 class="text-muted mb-1">信用等级</h6>
                            <span class="badge bg-{{ supplier.get_credit_rating_color }} fs-5 px-3 py-2">
                                {{ supplier.credit_rating }}级
                            </span>
                        </div>
                        {% endif %}

                        <!-- 综合评分 -->
                        {% if supplier.get_overall_rating %}
                        <div class="mb-3 text-center">
                            <h6 class="text-muted mb-1">综合评分</h6>
                            <div class="fs-4 text-warning">
                                {{ supplier.get_rating_stars }}
                            </div>
                        </div>
                        {% endif %}

                        <!-- 详细评分 -->
                        <div class="row text-center">
                            <div class="col-4">
                                <div class="border rounded p-2">
                                    <div class="text-primary fw-bold">{{ supplier.service_rating|default:"--" }}</div>
                                    <small class="text-muted">服务</small>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="border rounded p-2">
                                    <div class="text-success fw-bold">{{ supplier.quality_rating|default:"--" }}</div>
                                    <small class="text-muted">质量</small>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="border rounded p-2">
                                    <div class="text-info fw-bold">{{ supplier.delivery_rating|default:"--" }}</div>
                                    <small class="text-muted">交付</small>
                                </div>
                            </div>
                        </div>

                        <!-- 合作信息 -->
                        <div class="mt-3">
                            <div class="row">
                                <div class="col-6">
                                    <small class="text-muted">合作次数:</small>
                                    <div class="fw-bold">{{ supplier.cooperation_count }} 次</div>
                                </div>
                                <div class="col-6">
                                    <small class="text-muted">最后合作:</small>
                                    <div class="fw-bold">
                                        {% if supplier.last_cooperation_date %}
                                        {{ supplier.last_cooperation_date|date:"Y-m-d" }}
                                        {% else %}
                                        --
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 评价操作按钮 -->
                        <div class="mt-3 d-grid">
                            <button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal"
                                data-bs-target="#ratingModal">
                                <i class="fas fa-star me-1"></i>评价供应商
                            </button>
                        </div>
                    </div>
                </div>

                <!-- 快速操作 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-bolt me-2"></i>快速操作
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="{% url 'assets:asset_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-primary">
                                <i class="fas fa-plus me-2"></i>添加资产
                            </a>
                            <a href="{% url 'assets:consumable_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-warning">
                                <i class="fas fa-boxes me-2"></i>添加耗材
                            </a>
                            <a href="{% url 'assets:contract_create' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-success">
                                <i class="fas fa-file-contract me-2"></i>创建合同
                            </a>
                            <hr class="my-2">
                            <a href="{% url 'assets:asset_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-info btn-sm">
                                <i class="fas fa-cube me-2"></i>查看供应商资产
                            </a>
                            <a href="{% url 'assets:consumable_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-info btn-sm">
                                <i class="fas fa-boxes me-2"></i>查看供应商耗材
                            </a>
                            <a href="{% url 'assets:contract_list' %}?supplier={{ supplier.pk }}"
                                class="btn btn-outline-info btn-sm">
                                <i class="fas fa-file-contract me-2"></i>查看相关合同
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 联系信息 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-address-book me-2"></i>联系信息
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if supplier.contact_person %}
                        <p class="mb-2">
                            <i class="fas fa-user me-2 text-primary"></i>
                            <strong>联系人:</strong> {{ supplier.contact_person }}
                        </p>
                        {% endif %}
                        {% if supplier.phone %}
                        <p class="mb-2">
                            <i class="fas fa-phone me-2 text-success"></i>
                            <strong>电话:</strong>
                            <a href="tel:{{ supplier.phone }}" class="text-decoration-none">{{ supplier.phone }}</a>
                        </p>
                        {% endif %}
                        {% if supplier.email %}
                        <p class="mb-2">
                            <i class="fas fa-envelope me-2 text-info"></i>
                            <strong>邮箱:</strong>
                            <a href="mailto:{{ supplier.email }}" class="text-decoration-none">{{ supplier.email }}</a>
                        </p>
                        {% endif %}
                        {% if supplier.address %}
                        <p class="mb-0">
                            <i class="fas fa-map-marker-alt me-2 text-danger"></i>
                            <strong>地址:</strong> {{ supplier.address }}
                        </p>
                        {% endif %}

                        {% if not supplier.contact_person and not supplier.phone and not supplier.email and not supplier.address %}
                        <div class="text-muted text-center py-2">
                            <i class="fas fa-address-book fa-2x mb-2"></i>
                            <p class="mb-0">暂无联系信息</p>
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 详细统计信息 -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-chart-bar me-2"></i>详细统计
                        </h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-borderless table-sm">
                            <tr>
                                <td class="text-muted">供应商ID:</td>
                                <td>
                                    <code>SUP-{{ supplier.pk|stringformat:"05d" }}</code>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">资产总值:</td>
                                <td>
                                    <i class="fas fa-coins me-1 text-success"></i>
                                    <span class="text-success fw-bold">¥{{ total_asset_value|floatformat:2|default:"0.00" }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">合同总额:</td>
                                <td>
                                    <i class="fas fa-file-invoice-dollar me-1 text-info"></i>
                                    <span class="text-info fw-bold">¥{{ total_contract_value|floatformat:2|default:"0.00" }}</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">在用资产:</td>
                                <td>
                                    <i class="fas fa-play me-1 text-success"></i>
                                    {{ assets_in_use_count|default:0 }} 个
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">闲置资产:</td>
                                <td>
                                    <i class="fas fa-pause me-1 text-secondary"></i>
                                    {{ assets_idle_count|default:0 }} 个
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">生效合同:</td>
                                <td>
                                    <i class="fas fa-check me-1 text-success"></i>
                                    {{ active_contracts_count|default:0 }} 个
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted">合作时长:</td>
                                <td>
                                    <i class="fas fa-handshake me-1 text-muted"></i>
                                    {{ supplier.created_at|timesince }}
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 上传资质文件模态框 -->
<div class="modal fade" id="uploadQualificationModal" tabindex="-1" aria-labelledby="uploadQualificationModalLabel"
    aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="uploadQualificationModalLabel">
                    <i class="fas fa-upload me-2"></i>上传资质文件
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="uploadQualificationForm" method="post" enctype="multipart/form-data">
                <div class="modal-body">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ upload_form.document_type.id_for_label }}"
                                    class="form-label">文件类型</label>
                                {{ upload_form.document_type }}
                                {% if upload_form.document_type.errors %}<div class="invalid-feedback d-block">{% for error in upload_form.document_type.errors %}{{ error }}{% endfor %}</div>{% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ upload_form.title.id_for_label }}" class="form-label">文件名称</label>
                                {{ upload_form.title }}
                                {% if upload_form.title.errors %}<div class="invalid-feedback d-block">{% for error in upload_form.title.errors %}{{ error }}{% endfor %}</div>{% endif %}
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="{{ upload_form.file.id_for_label }}" class="form-label">选择文件</label>
                        {{ upload_form.file }}
                        <div class="form-text">支持格式：PDF、DOC、DOCX、JPG、PNG，最大10MB</div>
                        {% if upload_form.file.errors %}<div class="invalid-feedback d-block">{% for error in upload_form.file.errors %}{{ error }}{% endfor %}</div>{% endif %}
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ upload_form.expiry_date.id_for_label }}" class="form-label">有效期</label>
                                {{ upload_form.expiry_date }}
                                {% if upload_form.expiry_date.errors %}<div class="invalid-feedback d-block">{% for error in upload_form.expiry_date.errors %}{{ error }}{% endfor %}</div>{% endif %}

                                <!-- 长期有效选项 -->
                                <div class="form-check mt-2">
                                    {{ upload_form.is_permanent }}
                                    <label class="form-check-label" for="{{ upload_form.is_permanent.id_for_label }}">
                                        长期有效
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ upload_form.description.id_for_label }}" class="form-label">文件描述</label>
                                {{ upload_form.description }}
                                {% if upload_form.description.errors %}<div class="invalid-feedback d-block">{% for error in upload_form.description.errors %}{{ error }}{% endfor %}</div>{% endif %}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-upload me-1"></i>上传文件
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 评价供应商模态框 -->
<div class="modal fade" id="ratingModal" tabindex="-1" aria-labelledby="ratingModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="ratingModalLabel">
                    <i class="fas fa-star me-2"></i>评价供应商 - {{ supplier.name }}
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="ratingForm" method="post">
                <div class="modal-body">
                    {% csrf_token %}
                    <input type="hidden" name="supplier_id" value="{{ supplier.pk }}">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">服务评分</label>
                                <div class="rating-input" data-rating="service_rating">
                                    <input type="hidden" name="service_rating"
                                        value="{{ supplier.service_rating|default:'' }}">
                                    <div class="stars">
                                        <i class="fas fa-star" data-value="1"></i>
                                        <i class="fas fa-star" data-value="2"></i>
                                        <i class="fas fa-star" data-value="3"></i>
                                        <i class="fas fa-star" data-value="4"></i>
                                        <i class="fas fa-star" data-value="5"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">质量评分</label>
                                <div class="rating-input" data-rating="quality_rating">
                                    <input type="hidden" name="quality_rating"
                                        value="{{ supplier.quality_rating|default:'' }}">
                                    <div class="stars">
                                        <i class="fas fa-star" data-value="1"></i>
                                        <i class="fas fa-star" data-value="2"></i>
                                        <i class="fas fa-star" data-value="3"></i>
                                        <i class="fas fa-star" data-value="4"></i>
                                        <i class="fas fa-star" data-value="5"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">交付评分</label>
                                <div class="rating-input" data-rating="delivery_rating">
                                    <input type="hidden" name="delivery_rating"
                                        value="{{ supplier.delivery_rating|default:'' }}">
                                    <div class="stars">
                                        <i class="fas fa-star" data-value="1"></i>
                                        <i class="fas fa-star" data-value="2"></i>
                                        <i class="fas fa-star" data-value="3"></i>
                                        <i class="fas fa-star" data-value="4"></i>
                                        <i class="fas fa-star" data-value="5"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">信用等级</label>
                                <select name="credit_rating" class="form-select">
                                    <option value="">请选择信用等级</option>
                                    <option value="AAA" {% if supplier.credit_rating == "AAA" %}selected{% endif %}>AAA级</option>
                                    <option value="AA" {% if supplier.credit_rating == "AA" %}selected{% endif %}>AA级</option>
                                    <option value="A" {% if supplier.credit_rating == "A" %}selected{% endif %}>A级</option>
                                    <option value="BBB" {% if supplier.credit_rating == "BBB" %}selected{% endif %}>BBB级</option>
                                    <option value="BB" {% if supplier.credit_rating == "BB" %}selected{% endif %}>BB级</option>
                                    <option value="B" {% if supplier.credit_rating == "B" %}selected{% endif %}>B级</option>
                                    <option value="CCC" {% if supplier.credit_rating == "CCC" %}selected{% endif %}>CCC级</option>
                                    <option value="CC" {% if supplier.credit_rating == "CC" %}selected{% endif %}>CC级</option>
                                    <option value="C" {% if supplier.credit_rating == "C" %}selected{% endif %}>C级</option>
                                    <option value="D" {% if supplier.credit_rating == "D" %}selected{% endif %}>D级</option>
                                    </option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="is_preferred" id="is_preferred" {% if supplier.is_preferred %}checked{% endif %}>
                                <label class="form-check-label" for="is_preferred">
                                    <i class="fas fa-crown text-warning me-1"></i>设为优选供应商
                                </label>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="is_blacklisted"
                                    id="is_blacklisted" {% if supplier.is_blacklisted %}checked{% endif %}>
                                <label class="form-check-label" for="is_blacklisted">
                                    <i class="fas fa-ban text-danger me-1"></i>加入黑名单
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3" id="blacklist_reason_div" {% if not supplier.is_blacklisted %}style="display:none;"{% endif %}>
                        <label class="form-label">黑名单原因</label>
                        <textarea name="blacklist_reason" class="form-control" rows="3"
                            placeholder="请输入加入黑名单的原因">{{ supplier.blacklist_reason }}</textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i>保存评价
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const form = document.getElementById('uploadQualificationForm');
        if (!form) return; // 页面没有该表单时直接返回，避免空引用错误

        // 选中文件时，自动填充标题
        const fileInput = form.querySelector('input[name="file"]');
        const titleInput = form.querySelector('input[name="title"]');
        if (fileInput && titleInput) {
            fileInput.addEventListener('change', function () {
                const f = this.files && this.files[0];
                if (f && (!titleInput.value || titleInput.value.trim() === '')) {
                    titleInput.value = f.name;
                }
            });
        }

        // 上传资质文件表单处理（参考合同上传的交互：禁用按钮并显示加载中）
        form.addEventListener('submit', function (e) {
            e.preventDefault();

            const formData = new FormData(form);
            // 追加供应商ID，后端需要此字段
            formData.append('supplier_id', '{{ supplier.pk }}');

            const submitBtn = form.querySelector('button[type="submit"]');
            const originalText = submitBtn ? submitBtn.innerHTML : '';
            if (submitBtn) {
                submitBtn.disabled = true;
                submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>上传中...';
            }

            // 基本前端校验：必须选择文件
            const fileInputEl = form.querySelector('input[name="file"]');
            if (!fileInputEl || !fileInputEl.files || !fileInputEl.files.length) {
                alert('请先选择要上传的文件');
                if (submitBtn) {
                    submitBtn.disabled = false;
                    submitBtn.innerHTML = originalText;
                }
                return;
            }

            fetch("{% url 'assets:supplier_qualification_upload' %}", {
                method: 'POST',
                body: formData,
                headers: {
                    'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value,
                },
            })
                .then(async (response) => {
                    const text = await response.text();
                    let data = null;
                    try { data = JSON.parse(text); } catch (e) { /* 非 JSON 响应 */ }

                    if (response.ok) {
                        if (data && data.success === true) {
                            location.reload();
                        } else {
                            // 有时后端返回非 JSON 或未带 success 字段，仍视为成功并刷新
                            location.reload();
                        }
                        return;
                    }

                    // 非 2xx
                    if (data && data.errors) {
                        let errorMessage = '上传失败：';
                        const errors = typeof data.errors === 'string' ? JSON.parse(data.errors) : data.errors;
                        for (const field in errors) {
                            errorMessage += `\n${field}: ${errors[field].join(', ')}`;
                        }
                        alert(errorMessage);
                    } else {
                        alert('上传失败：' + (data && data.error ? data.error : text || '未知错误'));
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('上传失败，请重试');
                })
                .finally(() => {
                    if (submitBtn) {
                        submitBtn.disabled = false;
                        submitBtn.innerHTML = originalText;
                    }
                });
        });
    });

    // 长期有效选项处理（加空值判断）
    (function() {
        const isPermanent = document.getElementById('id_is_permanent');
        const expiryDateField = document.getElementById('id_expiry_date');
        if (!isPermanent || !expiryDateField) return;

        const syncExpiryState = () => {
            if (isPermanent.checked) {
                expiryDateField.disabled = true;
                expiryDateField.value = '';
            } else {
                expiryDateField.disabled = false;
            }
        };
        // 初始化
        syncExpiryState();
        // 监听变化
        isPermanent.addEventListener('change', syncExpiryState);
    })();

    // 星级评分处理
    document.querySelectorAll('.rating-input').forEach(function (ratingDiv) {
        const stars = ratingDiv.querySelectorAll('.stars i');
        const hiddenInput = ratingDiv.querySelector('input[type="hidden"]');
        const currentRating = parseFloat(hiddenInput.value) || 0;

        // 初始化显示
        updateStars(stars, currentRating);

        stars.forEach(function (star, index) {
            star.addEventListener('click', function () {
                const rating = index + 1;
                hiddenInput.value = rating;
                updateStars(stars, rating);
            });

            star.addEventListener('mouseover', function () {
                updateStars(stars, index + 1);
            });
        });

        ratingDiv.addEventListener('mouseleave', function () {
            updateStars(stars, parseFloat(hiddenInput.value) || 0);
        });
    });

    function updateStars(stars, rating) {
        stars.forEach(function (star, index) {
            if (index < rating) {
                star.style.color = '#ffc107';
            } else {
                star.style.color = '#e9ecef';
            }
        });
    }

    // 黑名单选项处理
    document.getElementById('is_blacklisted').addEventListener('change', function (e) {
        const reasonDiv = document.getElementById('blacklist_reason_div');
        if (e.target.checked) {
            reasonDiv.style.display = 'block';
        } else {
            reasonDiv.style.display = 'none';
        }
    });

    // 评价表单提交处理
    document.getElementById('ratingForm').addEventListener('submit', function (e) {
        e.preventDefault();

        const formData = new FormData(this);

        fetch(window.location.href, {
            method: 'POST',
            body: formData,
            headers: {
                'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value,
            },
        })
            .then(response => {
                if (response.ok) {
                    location.reload();
                } else {
                    alert('评价提交失败，请重试');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('评价提交失败，请重试');
            });
    });
</script>
{% endblock %}